#include "ActorOilbarrel.h"

ActorOilbarrel::ActorOilbarrel(CL_GraphicContext gc)
{
	grafic = new SpriteOilbarrel(gc);
	setPosition(20, 608);
	states[0] = new State(CL_Vec2<int>(20, 608),CL_Vec2<int>(270,608),barrelRight);
	states[1] = new State(CL_Vec2<int>(270,608),CL_Vec2<int>(470,596),barrelRightDown);
	states[2] = new State(CL_Vec2<int>(470,596),CL_Vec2<int>(270,608),barrelLeftDown);
	states[3] = new State(CL_Vec2<int>(270,608),CL_Vec2<int>(20, 608),barrelLeft);
	actualstate = 0;
	updowndelay = 0;
	playerlost = false;
}

void ActorOilbarrel::processoilbarrel()
{
	grafic->drawoilbarrel(getPosition());
}

void ActorOilbarrel::processflame()
{
	switch(states[actualstate]->getDirection())
	{
	case barrelRight:
		setPosition(getPosition().x+1, getPosition().y);
		grafic->drawflame(getPosition());
		break;
	case barrelRightDown:
		if(updowndelay == 15)
		{
			setPosition(getPosition().x+1, getPosition().y-1);
			updowndelay=0;
		}
		else
		{
			setPosition(getPosition().x+1, getPosition().y);
			updowndelay++;
		}
		grafic->drawflame(getPosition());
		break;
	case barrelLeftDown:
		if(updowndelay == 15)
		{
			setPosition(getPosition().x-1, getPosition().y+1);
			updowndelay=0;
		}
		else
		{
			setPosition(getPosition().x-1, getPosition().y);
			updowndelay++;
		}
		grafic->drawflame(getPosition());
		break;
	case barrelLeft:
		setPosition(getPosition().x-1, getPosition().y);
		grafic->drawflame(getPosition());
		break;
	}

	

	if(CollisionDetection::giveme()->enofstate(states[actualstate]->getEscapePoint(), getPosition()))
	{
		actualstate++;
		if(actualstate == 4)
		{
			actualstate = 0;
		}
	}

	if(CollisionDetection::giveme()->mariobarrel(CL_Vec2<int>(getPosition().x/*+6*/, getPosition().y)))
	{
		playerlost = true;
	}
}

bool ActorOilbarrel::hasplayerlost()
{
	return playerlost;
}

void ActorOilbarrel::reset()
{
	setPosition(20, 608);
	actualstate = 0;
	updowndelay = 0;
	playerlost = false;
}

ActorOilbarrel::~ActorOilbarrel()
{
	delete grafic;
}